/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.api.model;

import org.proteusframework.util.Assert;


/**
 * Simplest implementation of a fully qualified named object. Each fully qualified named object consists of a
 * namespace and an ID that must be guaranteed unique within the given namespace.
 *
 * @author Tacoma Four
 */
public class QualifiedName implements IQualifiedName
{
    /**
     * Immutable, non-null namespace.
     */
    protected final String namespace;

    /**
     * Immutable, non-null ID guaranteed to be unique within the namespace.
     */
    protected final String id;

    private final String refId;

    /**
     * Constructs a qualified name.
     *
     * @param namespace Non-null, immutable namespace
     * @param id        Non-null, immutable ID that is guaranteed unique within the namespace
     */
    public QualifiedName(String namespace, String id)
    {
        this(namespace, id, null);
    }

    /**
     * Constructs a qualified name.
     *
     * @param namespace Non-null, immutable namespace
     * @param id        Non-null, immutable ID that is guaranteed unique within the namespace
     * @param refId     Optional (may be null) reference ID
     */
    public QualifiedName(String namespace, String id, String refId)
    {
        Assert.parameterNotNull(namespace, "Parameter 'namespace' must not be null");
        Assert.parameterNotNull(id, "Parameter 'id' must not be null");
        this.namespace = namespace;
        this.id = id;
        this.refId = refId;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getNamespace()
    {
        return namespace;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getId()
    {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getRefId()
    {
        return refId;
    }

    /**
     * Equality id determined by verifying that the namespace and ID are case-sensitive matches.
     *
     * @param o Object to check for equality against this instance
     * @return true, if the objects are equal
     */
    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (!(o instanceof QualifiedName)) return false;

        QualifiedName that = (QualifiedName) o;

        if (!id.equals(that.id)) return false;
        if (!namespace.equals(that.namespace)) return false;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        int result = namespace.hashCode();
        result = 31 * result + id.hashCode();
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString()
    {
        return "QualifiedName{" +
                "namespace='" + namespace + '\'' +
                ", id='" + id + '\'' +
                ", refId='" + refId + '\'' +
                '}';
    }
}
